package com.smartens.boit.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.smartens.boit.R;
import com.smartens.boit.adapter.AbnormalAdapter;
import com.smartens.boit.adapter.NormalAdapter;
import com.smartens.boit.base.BaseActivity;
import com.smartens.boit.bean.DevicesList;
import com.smartens.boit.bean.EquipmentEntity;
import com.smartens.boit.bean.LightDetailBean;
import com.smartens.boit.bean.OffLineBean;
import com.smartens.boit.util.HttpHelper;

import java.util.ArrayList;
import java.util.List;

import okhttp3.internal.http2.Header;

/**
 * @Description 离线设备列表
 * @Author songtt
 * @Date 2021/3/4
 */

public class AbnormalActivity extends BaseActivity {
    private RecyclerView recyclerView;
    private AbnormalAdapter recycleAdapter;
    private LinearLayoutManager mLinearLayoutManager;
    private List<DevicesList> mList;
    private ImageView back;
    private HttpHelper helper;
    private TextView empty;

    private SwipeRefreshLayout mRefreshView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        helper = HttpHelper.getInstance();
        setContentView(R.layout.activity_abnormal);
        GetOffLine(3);
        recyclerView = findViewById(R.id.recycler);
        back = findViewById(R.id.tv_back);
        empty = findViewById(R.id.empty);

        /**
         * 下拉刷新
         */
        mRefreshView = findViewById(R.id.swipefresh);
        mRefreshView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                /**
                 * 延时1秒清除刷新样式
                 */
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        GetOffLine(3);
                        mRefreshView.setRefreshing(false);
                    }
                },1000);
            }
        });

        recyclerView = findViewById(R.id.recycler);

        mLinearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mList= new ArrayList<>();
        initView();
        recycleAdapter = new AbnormalAdapter(mList);
        //设置布局管理器
        recyclerView.setLayoutManager(mLinearLayoutManager);
        //设置Adapter
        recyclerView.setAdapter(recycleAdapter);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AbnormalActivity.this.finish();
            }
        });


    }

    @Override
    protected void initView() {
        // 当无数据时加载页面不同
        if (mList.size() > 0) {
            empty.setVisibility(View.VISIBLE);
        } else {
            empty.setVisibility(View.GONE);
        }
    }

    @Override
    protected void initData() {

    }


    public void GetOffLine(int status) {

        helper.GetDevicesList(status, new HttpHelper.HttpCallBack<DevicesList>() {
            @Override
            public void onSuccess(List<DevicesList> result) {
              //   Log.v("请求成功", String.valueOf(result.size()));
                   // mList = result;
                    mList.clear();
                    mList.addAll(result);
                    recyclerView.post(new Runnable() {
                        @Override
                        public void run() {
                          //  recycleAdapter.getData(mList);
                            recycleAdapter.notifyDataSetChanged();
                        }
                    });
            }

            @Override
            public void onFailure(String message) {
               //  Log.v("1122", message);
            }

        });
    }

}